import { mergeAll, type } from 'ramda';
import React, { ReactElement } from 'react';
import {
  Text,
  TextProps as NativeTextProps,
  StyleSheet,
  View
} from 'react-native';
import { isIOS } from '../../constant';

interface TextProps extends NativeTextProps {
  children: any;
}

const CustomText = (props: TextProps): ReactElement => {
  const { style = {}, children } = props;
  let flattenStyle: any = StyleSheet.flatten(style);
  flattenStyle = mergeAll(
    type(flattenStyle) === 'Array' ? flattenStyle : [flattenStyle]
  );
  const { fontSize, lineHeight, color, fontWeight } = flattenStyle;
  const height = lineHeight || fontSize || 0;
  return (
    <View
      style={{
        height: isIOS ? height + 1 : height,
        justifyContent: 'center',
        overflow: 'hidden',
        ...flattenStyle
      }}
    >
      <Text {...props} style={{ fontSize, lineHeight, color, fontWeight }}>
        {children}
      </Text>
    </View>
  );
};

export default CustomText;
